package com.fenghongzhang.day016;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private SeekBar seekBar;
    private TextView title;
    private TextView atrist;
    private LinearLayout ll;
    private Button pause;
    private Button restart;
    private Button stop;
    private Button pre;
    private Button next;
    private Button model;
    private MusicService service1;
    private  MusicService.MyBinder binder;
    private List<Music> list;
    private Timer timer;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        lv = (ListView) findViewById(R.id.lv);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        title = (TextView) findViewById(R.id.title);
        atrist = (TextView) findViewById(R.id.atrist);
        ll = (LinearLayout) findViewById(R.id.ll);
        pause = (Button) findViewById(R.id.pause);
        restart = (Button) findViewById(R.id.restart);
        stop = (Button) findViewById(R.id.stop);
        pre = (Button) findViewById(R.id.pre);
        next = (Button) findViewById(R.id.next);
        model = (Button) findViewById(R.id.model);


        //请求权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
        }

        //点击事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Log.i(TAG, "onItemClick: ");
                service1.play(position);
                //改进度的值
                if(timer == null){
                    timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            seekBar.setMax(service1.getMax());//0
                            seekBar.setProgress(service1.getCorrectPosition());

                            //设置标题
                            int index = service1.getIndex();
                            final String title11 = list.get(index).getTitle();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    title.setText(title11);
                                }
                            });

                        }
                    },0,1000);
                }

            }
        });



        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser){
                    service1.setProgress(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        //启动服务,
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);


        ServiceConnection serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                binder = (MusicService.MyBinder) service;
                //拿到服务对象
                 service1 = binder.getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        //调用服务中的方法
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);


    }
    @Override
    public void onRequestPermissionsResult ( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //直接去sd卡取到数据源
            Log.i(TAG, "onRequestPermissionsResult: ");
            list = MusicUtil.getData(this);
            //创建适配器.
            MusicAdapter musicAdapter = new MusicAdapter(this, list);
            lv.setAdapter(musicAdapter);
        } else {
            //
        }
    }

    public void pause(View view) {
//        binder.callPause();
        service1.pause();
    }

    public void restart(View view) {
//        binder.callRestart();
        service1.restart();
    }

    public void stop(View view) {
    }

    public void pre(View view) {
    }

    public void next(View view) {
    }

    public void model(View view) {
        //弹出窗口
        final PopupWindow popupWindow = new PopupWindow();
        popupWindow.setWidth(200);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        View inflate = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);
        TextView single = inflate.findViewById(R.id.pop_single);
        single.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "单曲", Toast.LENGTH_SHORT).show();
//                playModel = MusicUtil.PLAY_MODEL_SINGLE;
                service1.setPlayModel(MusicUtil.PLAY_MODEL_SINGLE);
                popupWindow.dismiss();
            }
        });

        TextView random = inflate.findViewById(R.id.pop_random);
        random.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                playModel = MusicUtil.PLAY_MODEL_RANDOM;
                service1.setPlayModel(MusicUtil.PLAY_MODEL_RANDOM);
                Toast.makeText(MainActivity.this, "随机", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });


        TextView order = inflate.findViewById(R.id.pop_order);
        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                playModel = MusicUtil.PLAY_MODEL_ORDER;
                service1.setPlayModel(MusicUtil.PLAY_MODEL_ORDER);
                Toast.makeText(MainActivity.this, "顺序", Toast.LENGTH_SHORT).show();
                popupWindow.dismiss();
            }
        });

        //添加内容
        popupWindow.setContentView(inflate);

        //点击外部pop可以取消
        popupWindow.setOutsideTouchable(true);

        //在哪里显示
        popupWindow.showAsDropDown(model,0,-350);
    }
}
